<?php

namespace app\controllers;

use app\models\Enterprise;
use app\models\NormalUser;
use app\models\Job;
use app\modules\admin\models\Finance;
use app\modules\admin\models\Message;
use Yii;
use yii\web\Controller;
use yii\web\UploadedFile;
use yii\data\Pagination;
/*用户中心控制器
 *
 *
 */

class UserController extends Controller
{
    public $defaultAction = 'index';

    public function actionIndex()
    {
        //个人中心
        $session = Yii::$app->session;
        if (is_null($session['User']['token'])) {
            return $this->redirect(['login/login']);
        }
        $message = Message::find()->where('uid=:uid and is_read=0',[':uid'=>$session['User']['id']])->all();
        $this->layout = 'user';
        return $this->render('index',['message'=>$message]);
    }

    public function actionModify()
    {
        //用户简历信息修改 完善
        $this->layout = 'user';
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post('NormalUser');
            $DM = NormalUser::findOne(Yii::$app->session['User']['id']);
            $Filemodel = $DM->head_img = UploadedFile::getInstance($DM, 'head_img');
            $savePath = 'upload/' . time() . '.' . $Filemodel->extension;
            $Filemodel->saveAs($savePath);
            $post['head_img'] = $savePath;
            $DM->updateAttributes($post);
            echo "<script>alert('简历修改成功！')</script>";
        }
        $show = NormalUser::find()->where(['id' => Yii::$app->session['User']['id']])->one();
        $show->scenario = 'modify';
        return $this->render('modify', ['model' => $show]);
    }


    public function actionResetPassword()
    {
        //后台重置密码
        $this->layout = 'user';
        $model = new NormalUser();
        $model->scenario = 'resetPassword';
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post('NormalUser');
            $DM = NormalUser::find()->where(['id' => Yii::$app->session['User']['id']])->one();
            $DM->password = $post['password1'];
            if ($DM->save(false)) {
                echo "<script>alert('修改成功');location.href('index.php?r=user');</script>";
            } else
                echo "<script>alert('修改失败请重试！');location.href('index.php?r=user/reset-password');</script>";
        }
        return $this->render('resetPassword', ['model' => $model]);
    }

    public function actionMessage(){
        //消息列表
        //kind=1 标记所有uid 为已读 kind=0 页面删除所有（数据库中保留）status置为0
        $this->layout='user';
        $kind = Yii::$app->request->get('kind');
        $message = Message::find()->andwhere(['uid' => Yii::$app->session['User']['id']])->andwhere(['status'=>1])->orderBy(['id'=>SORT_DESC])->all();
        if($kind==2){
            //置status为0
            foreach ($message as $obj){
                $obj->status=0;
                $obj->save(false);
                echo "<script>alert('删除成功');location.href='index.php?r=user/message'</script>";
            }
        }
        if($kind==1){
            //置is_read 为 1
            foreach ($message as $obj){
                $obj->is_read=1;
                $obj->save(false);
                echo "<script>alert('标记成功');location.href='index.php?r=user/message'</script>";
            }
        }

        return $this->render('message',['message'=>$message]);
    }

    public function actionOrderSubmission(){
        //提交订单
        //列出用户所有 job
        $this->layout = 'user';
        $id = Yii::$app->request->get('id');
        $session = Yii::$app->session;
        $finance = new Finance();
        if($id) {
            $job = Job::find()->where(['id'=>$id])->one();
            $finance->uid = $session['User']['id'];//填入用户id
            $finance->jid = $id;//填入工作id
            $finance->price = $job->price;
            $finance->update_time = time();//订单创建时间
            $finance->save(false);
            echo "<script>alert('提交申请成功！等待管理员审核！');location.href='index.php?r=user/deal-list'</script>";
        }
        $job = Job::find()->andwhere(['uid'=>$session['User']['id']])->andwhere(['is_match'=>1]);
        $countQuery = clone $job;
        $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' =>'6']);
        $model = $job->offset($page->offset)->limit($page->limit)->all();
        return $this->render('orderSubmission',['job'=>$model,'finance'=>$finance,'page'=>$page]);
    }

    public function actionDealList(){
        //交易记录(交易列表) 查询finance中uid为session id的数据
        $this->layout = 'user';
        $session = Yii::$app->session;
        $finance = Finance::find()->andwhere(['uid'=>$session['User']['id']])->andwhere(['status'=>1]);
        $countQuery = clone $finance;
        $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' =>'6']);
        $model = $finance->offset($page->offset)->limit($page->limit)->all();
        return $this->render('dealList',['model'=>$model,'page'=>$page]);
    }

    public function actionDealDelete(){
        //用户删除订单 将finance的status置为 0
        $id = Yii::$app->request->get('id');
        $finance = Finance::find()->where(['id'=>$id])->one();
        $finance->status = 0;
        $finance->save(false);
        echo "<script>alert('删除订单成功！');location.href='index.php?r=user/deal-list'</script>";
    }

    public function actionJobApply(){
        //提交工作申请
        //提交后往job的message里加数据
        $this->layout = 'user';
        $session = Yii::$app->session;
        $job = Job::find()->where(['id'=>Yii::$app->request->get('id')])->one();
        if(Yii::$app->request->isPost){
            //往job里面写message
            $post = Yii::$app->request->post('Job');
            $job->uid = $session['User']['id'];
            $job->message = $post['message'];
            $job->save(false);
            echo "<script>alert('申请成功！等待后台审核。');location.href='index.php?r=user/order-submission'</script>";
        }
        return $this->render('jobApply',['job'=>$job]);
    }


    public function actionHelp()
    {
        //帮助页面
        $this->layout = 'user';
        return $this->render('help');
    }

    public function actionResume()
    {
        //用户简历展示
        $information = NormalUser::find()->where(['id' => Yii::$app->request->get('id')])->one();
        if (is_null($information)) {
            //查询成功后把查询到的数据放在information中
            echo "<script>alert('用户不存在！');location.href='index.php?r=index/index'</script>";
        } else {
            return $this->renderPartial('resume', ['information' => $information]);
        }
    }

    public function actionEnterpriseResume()
    {
        //查询成功后把查询到的数据放在information中
        $information = Enterprise::find()->where(['id' => Yii::$app->request->get('id')])->one();
        if (is_null($information)) {
            echo "<script>alert('用户数据不存在！');location.href='index.php?r=index/index'</script>";
        } else {
            return $this->renderPartial('enterpriseResume', ['information' => $information]);
        }
    }

    public function actionJobResume(){
        //显示工作的具体要求
        $job = Job::find()->where(['id'=>Yii::$app->request->get('id')])->one();
        if(is_null($job)){
            echo "<script>alert('工作信息不存在！');location.href='index.php?r=user/list-job'</script>";
        }else
            return $this->renderPartial('jobResume',['job'=>$job]);
    }

    public function actionLogOut()
    {
        //退出登陆
        unset(Yii::$app->session['User']);
        return $this->redirect('index.php?r=index/index');
    }
}